var adPublish = new XFApp.client();
XFApp.client.prototype.adPublish = {
	successCode : "10000",
	submitAdSpaces : function(type) {
		var params = [];
		$("input").each(function() {
			if(!!$(this).attr("name")){
				var param = {name:'',value:''};
				param.name=$(this).attr("name");
				param.value= $(this).val();
				if(!(type == 2 && $(this).attr("name")=='id')){
					params.push(param);
				}
			}
		});
		$("select").each(function() {
			if(!!$(this).attr("name")){
				var param = {name:'',value:''};
				param.name=$(this).attr("name");
				param.value= $(this).val();
				params.push(param);
			}
		});
		
		webDialogUtil.simpleConfrimAlert("是否确认提交", function(){
			$.ajaxFileUpload({
				url : basePath + "/advertiseSpace/save",
				secureuri:false,
				fileElementId:'image',//file标签的id
				dataType: 'json',
				data : params,
				success: function (data, status) {
					if(data.code!=10000){
						webDialogUtil.simpleAlert(data.msg, "温馨提示");
					}else{
						webDialogUtil.simpleAlert("处理成功", "温馨提示");
						if(!!data.result){
							$("input[name='id']").val(data.result.id);
							$("#mod_btn").show();
							if(!!data.result.path){
								$("#img_display").attr("src",data.result.path);
							}
						}
					}
				},
				error: function (data, status, e) {
					webDialogUtil.simpleAlert("处理异常", "温馨提示");
				}
			});
		});
	},
	formValidate : function(){
		$("#editForm").validate({
	        rules: {
	        	targetUrl: {
	        		required:true,
	        		maxlength:255,
	        	},
	        	image: "required"
			  },
			 messages: {
				targetUrl: {
					required:"请输入跳转链接",
					maxlength:"长度超出限制",
				},
				image: "请选择图片"
	        
			  }
	    });
		$("#editForm").validate();
	},
	event : function() {
		_this = this;
		_this.formValidate();
		$("#submit_btn").on("click",function(){
			if($("#editForm").validate().form()){
				_this.submitAdSpaces(2);
			}
		});
		$("#mod_btn").on("click",function(){
			if($("#editForm").validate().form()){
				_this.submitAdSpaces(1);
			}
		});
		$("#mod_img").on("click",function(){
			$("#image_upload").html('<input type="file" name="image" style="width:100%" id="image" />');
		});
	},
};
$(function() {
	if(!!$("input[name='id']").val()){
		$("#mod_btn").show();
		$("#submit_btn").hide();
	}else{
		$("#mod_btn").hide();
	}
	adPublish.adPublish.event();
	
});